/*****************************************************************************
 *
 *              Main.c -- user main program
 *
 *****************************************************************************
 * FileName:        HardwareProfile.c
 * Dependencies:
 * Processor:       PIC18F
 * Compiler:        C18 02.20.00 or higher
 *                  PICC 9.50.00 or higher
 *                  C30 02.03.00 or higher
 * Linker:          MPLINK 03.40.00 or higher
 * Company:         Microchip Technology Incorporated
 *
 *
 *****************************************************************************/
 /*****************************************************************************
 *	NOTES GKRE
 *
 *****************************************************************************/

#include "GENp18.h"
#include "SYSp18.h"
#include "IOSp18.h"
#include "SPIp18.h"
#include "ADCp18.h"
#include "TTYp18.h"

#include "MPPp18.h"

#define OUT 0
#define IN 1


//-----------------------------------------------------------------------------
//
// APM_hal_init
//
// Performs basic board intialisation : ports
//
//-----------------------------------------------------------------------------

VOID APM_hal_init(VOID)
{
#if defined(__18F26K20)
	OSCCON = 0x72;		// Sleep mode with SLEEP instruction
						// Internal oscillator @ 16MHz
						// System clock select = internal oscillator block
	
	// Wait until HFINTOSC is stable
	while(OSCCONbits.IOFS == 0);
#endif

//disable WDT
WDTCONbits.SWDTEN = 0;

// Port A is digital
ADCON1 = 0xFF;

#if defined(__18F26K20)
	ANSEL = 0xE0;
#endif

// Pull ups on port B
INTCON2 = 0x00;
INTCON3 = 0x00;

// RA0 - Voltage measurement analog input
TRISAbits.TRISA0 = IN;

// RA1 - Function PB input
TRISAbits.TRISA1 = IN;

// RA2 - Run LED - 1 to light
PORTAbits.RA2 = 0;
TRISAbits.TRISA2 = OUT;

// RA3 to RA5 - Not used ==> outputs
TRISAbits.TRISA3 = OUT;
TRISAbits.TRISA4 = OUT;
TRISAbits.TRISA5 = OUT;

// RA6 - RA7 : Crystal oscillator - no initialization

// RB0 - RF interrupt - configure as input
TRISBbits.TRISB0 = IN;

// RB1 - I2C interrupt - configure as input
TRISBbits.TRISB1 = IN;

// RB2 - NC ==> output
TRISBbits.TRISB2 = OUT;

// RB3 - SCL ==> output
TRISBbits.TRISB3 = OUT;

// RB4 - SDA ==> output
TRISBbits.TRISB4 = OUT;

// RB5 - RF reset - configure as output
PORTBbits.RB5 = 1;
TRISBbits.TRISB5 = 0;

// RB6 - PGC
// RB7 - PGD

// RC0 - ADC CS - output
PORTCbits.RC0 = 1;
TRISCbits.TRISC0 = OUT;

// RC1 - RF CS - output
PORTCbits.RC1 = 1;
TRISCbits.TRISC1 = 0;

// RC2 - RF WAKE - output
PORTCbits.RC2 = 1;
TRISCbits.TRISC2 = 0;

// RC3 - SPI SCK - has to be set as an output - Done in SPIopen()
// RC4 - Used as SPI SDI has to be set as an input - Done in SPIopen()
// RC5 -  SPI SDO connectedt - Set as digital output - Done in SPIopen()
// RC6 - EUSART TXD
// RC7 - EUSART RXD

} // End of APM_hal_init()

